package org.edu.modules.research.rwork.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.research.rwork.dto.VeJkyMeetDto;
import org.edu.modules.research.rwork.entity.VeJkyMeet;
import org.edu.modules.research.rwork.entity.VeJkyMeetUser;
import org.edu.modules.research.rwork.service.VeJkyMeetService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.research.rwork.service.VeJkyMeetUserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

@Api(tags = {"会议表（会议，或调用OA会议模块）"})
@RestController
@RequestMapping("/research/veJkyMeet")
@Slf4j
public class VeJkyMeetController{

    @Autowired
    private VeJkyMeetService veJkyMeetService;


    @Autowired
    private VeJkyMeetUserService veJkyMeetUserService;

    @ApiOperation("分页查询会议表（会议，或调用OA会议模块）")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeJkyMeet> page) {
        QueryWrapper<VeJkyMeet> queryWrapper =  page.buildQueryWrapper(VeJkyMeet.class);

        if(page.getParams()!=null){
            if(page.getParams().get("apply")!=null){
                LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                queryWrapper.apply(" d.id in (select meet_id from ve_jky_meet_user ddd inner join ve_jky_user uuu on " +
                        " ddd.jky_user_id = uuu.id where uuu.user_id = {0}) ",sysUser.getId());
            }
        }
        return Result.OK(veJkyMeetService.pageDto(page, queryWrapper));
    }

    @ApiOperation("获取会议人员")
    @GetMapping("/findMeetUsers")
    public Result<?> findMeetUsers(String meetId) {
        return Result.OK(veJkyMeetUserService.findLitByMeetId(meetId));
    }

    @AutoLog(value = "会议表（会议，或调用OA会议模块）-分页列表查询")
    @ApiOperation(value="会议表（会议，或调用OA会议模块）-分页列表查询", notes="会议表（会议，或调用OA会议模块）-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeJkyMeet entity,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeJkyMeet> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeJkyMeet> page = new PageDto<VeJkyMeet>(pageNo, pageSize);
        IPage<VeJkyMeet> pageList = veJkyMeetService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        VeJkyMeet dto = veJkyMeetService.findDtoById(id);
        if(dto==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJkyMeetDto dto) {
        veJkyMeetService.insertOrUpdate(dto , true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value="编辑", notes="编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJkyMeetDto dto) {
        veJkyMeetService.insertOrUpdate(dto , false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        veJkyMeetService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value="更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        UpdateWrapper<VeJkyMeet> updateWrapper = new UpdateWrapper<>();
        if(statusDto.getIds()!=null && statusDto.getIds().size()>0){
            updateWrapper.set("status",statusDto.getStatus());
            updateWrapper.in("id",statusDto.getIds());
            veJkyMeetService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        }else{
            return Result.error("未选择!");
        }
    }

    @AutoLog(value = "审核")
    @ApiOperation(value="审核")
    @GetMapping(value = "/audit")
    public Result<?> audit(String id,Integer status) {
        LambdaUpdateWrapper<VeJkyMeet> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(VeJkyMeet::getStatus,status);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        updateWrapper.set(VeJkyMeet::getMarkUid,sysUser.getId());
        updateWrapper.set(VeJkyMeet::getMarkName,sysUser.getRealname());
        updateWrapper.set(VeJkyMeet::getMarkTime,new Date());
        updateWrapper.eq(VeJkyMeet::getId,id);
        veJkyMeetService.update(updateWrapper);
        return Result.OK("审核成功!");
    }


    @AutoLog(value = "更新考勤")
    @ApiOperation(value="更新考勤")
    @PostMapping(value = "/updateMeetUser")
    public Result<?> updateMeetUser(@RequestBody StatusDto statusDto) {
        LambdaUpdateWrapper<VeJkyMeetUser> updateWrapper = new LambdaUpdateWrapper<>();
        if(statusDto.getIds()!=null && statusDto.getIds().size()>0){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            updateWrapper.set(VeJkyMeetUser::getSignStatus,statusDto.getStatus());
            updateWrapper.set(VeJkyMeetUser::getCreateTime,new Date());
            updateWrapper.set(VeJkyMeetUser::getCreateName,sysUser.getRealname());
            updateWrapper.set(VeJkyMeetUser::getCreateUid,sysUser.getId());
            updateWrapper.in(VeJkyMeetUser::getId,statusDto.getIds());
            veJkyMeetUserService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        }else{
            return Result.error("未选择!");
        }
    }



}
